<?php 
session_start();
$sort = $_GET['sort'];
$filter = $_POST['filter_field']; if (!$filter) { $filter = $_GET['filter_field'];}
$filter_data = $_POST['dataa']; if (!$filter_data) { $filter_data = $_GET['dataa'];}

 $link = mysql_connect('localhost', 'kootenay', 'nonsense');
 if (!$link) {
  die ('Not connected : ' . mysql_error());
 }
 $db_selected = mysql_select_db('kootenay', $link);

 if (!$db_selected) {
  die ('Can\'t use foo : ' . mysql_error());
 }
 
 ///////////// erase this, just to get session cookie set
 
 if ($_GET['member_id'])
 {
 	//if ($_HTTP_REFERRER != "cjly.org") exit;
	
 	$member_id_getted = $_GET['member_id'];
	$screen_name_getted = $_GET['sn'];
  session_start();
  header("Cache-control: private");
  $_SESSION['memberID'] = $member_id_getted;
  $_SESSION['screenName'] = $screen_name_getted;
  //echo "debug  ".$_SESSION['memberID'];
 }
 
 /////////  USE THIS INSTEAD::::
 /////////  ALSO, GET SITE TO SEND USERNAME IN GET
 /////////   
 
// if ($_HTTP(refferrr) == "cjly.com")
// {
//   session_start();
//   header("Cache-control: private");
//   $_SESSION['login'] = 
// } 

//require "functions.php";

if ($filter) 
{
	if ($filter == "playbox") { $filter_clause = ' WHERE playbox_p LIKE "P"'; }
	if ($filter == "library") { $filter_clause = ' WHERE playbox_p is LIKE "L"'; }
	if ($filter == "creation_date") { $date1 = $_POST['data1'];$date2 = $_POST['data2'];
	$filter_clause = ' WHERE creation_date > "' . $date1.'" AND creation_date < "'.$date2.'"'; }
	if (($filter != "playbox" && $filter != "library") && ($filter != "creation_date"))
		{ $filter_clause = " WHERE `" . $filter . '` LIKE "%' . $filter_data . '%" AND superseded_by IS NULL OR `keywords` LIKE "%'.$filter_data.'%"'; } else { $filter_clause .= " AND superseded_by IS NULL "; }
}

if ($sort) {
$sort_clause = "ORDER BY left (ml_albums.creation_date, 10) DESC,`" . $_GET['sort'] . "` ASC ";
  } else { $sort_clause = "ORDER BY left (ml_albums.creation_date, 10) DESC, `section_id` ASC "; }

$query="SELECT * FROM ml_albums" . $filter_clause . " " . $sort_clause . " LIMIT 1000"; 

//echo $query;
//echo "debug testid: " . $_POST['testid'] . "debug testid2: " . $_POST['testid2'];


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>KCR Music Database </title>
<link href="css/style.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="autosuggest/js/bsn.AutoSuggest_2.1.3.js" charset="utf-8"></script>

<link rel="stylesheet" href="autosuggest/css/autosuggest_inquisitor.css" type="text/css" media="screen" charset="utf-8" />
<script type="text/javascript" src="calendar_picker.js"></script>
<script type="text/javascript">
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function clearField()
{
var fldVal = document.myform.dataa.value;
//alert (fldVal);
if (fldVal == " - Find by Artist, Keyword -") { document.myform.dataa.value = ""; }
}

function fillField()
{
var fldVal = document.myform.dataa.value;
//alert (fldVal);
if (fldVal == "") { document.myform.dataa.value = " - Find by Artist, Keyword -"; }
}
</script>


</head>

<body onload="document.testid.value = '';">
<script type="text/javascript" src="wz_tooltip/wz_tooltip.js"></script>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><a href="index.php" class="factTextHead"><u><strong>Music Database</strong></u></a><br /> <span class='sortCriteria'><br />&nbsp;<?php if ($filter) {echo" <b>$filter</b>, ";} echo " sorted by "; if ($sort) {echo " category, most recent";} else {echo " category, most recent";} ?></span></td>
    <td><form action="index.php" method= "get" name="filter" action="index.php">
    	
		  <font size="2">&nbsp; </font>
		  <select name="filter" id="filter" onchange="MM_jumpMenu('parent',this,0)">
           <option value="index.php">---- Show ----</option>
          <option value="index.php">most recent entries, all</option>
          <option value="index.php?filter_field=playbox" >most recent entries, playbox</option>
    	<option value="index.php?filter_field=library" >most recent entries, library</option>
		  
		  </select></form>
          <?php if ($_SESSION['memberID']) { echo "<br><div class='tdNewButton' width='150'><a href='edit_album.php?aid=xx'>+ Add a new album</a></div>"; echo "<div class='tdPrintButton'><a href='inventory.php' target='blank'>print playbox</a></div>"; } ?>
          
          </td><td><table width="300" border="0" cellspacing="0" cellpadding="0">
  <tr>
  <form method="post" action="edit_album.php" class="asholder" name='myform'>
    <td class="tdUnderline" nowrap="nowrap">

 <div id="wrapper" onmouseover="Tip('Type in words to see a list of albums that match, either by name or keyword. To see a longer list, hit Return after typing your words.')" onmouseout="UnTip()">
<div id="content">
	<input type="text" value=" - Find by Artist, Keyword -" id="testinput_xml" value="" class= "input_a" name="dataa" style="width:200px" onFocus="clearField();" onBlur="fillField();"></label>
    <small style="float:right">
    <input name="testid" type="hidden" id="testid" value="" style="font-size: 10px; width: 20px;"  /></small> 
    
     <input type="hidden" id="filter_field" value="artist" name="filter_field" /> 
</form>
</div>
</div> 
  
  

  </td>
  
 <form action="index.php" method="post" name="dateform" ><input type="hidden" name="filter_field" value="creation_date" id="filter_field_2" />
  <td class="tdUnderline">
  
  <table> 
<tr>
<td onmouseover="Tip('Choose a start and end date, then click the >> button to show only albums entered during that period.')" onmouseout="UnTip()"><input value=" - From Date -" name="data1" id="data1" type="text" size="10" maxlength="100" class= "input_s" onclick="displayDatePicker('data1');" /><br /><input name="data2" value=" - To Date -" id="data2" type="text" size="10" maxlength="100" class= "input_s" onclick="displayDatePicker('data2');" /></td><td valign="middle"><input name="Find" type="submit" value=">>" /></form></td></tr></table>
  

  </td>
  </tr>
</table>


</td>
  </tr>
</table>
<p>

</p>
<table width="90%" border="0" cellspacing="0" cellpadding="3">
  <tr>
    <td class="tdhedL">Category</td>
    <td class="tdhedL">Album</td>
    <td class="tdhedL">Artist</td>
    <td class="tdhedL">Date Added</td>
    <td class="tdhedL">Where</td>
  </tr>
 <?php  
 
$result= mysql_query($query); //echo $query;
while ($row= mysql_fetch_array($result, MYSQL_BOTH))
{
if ($x == 1) { $x = 0; } else { $x = 1; }
if ($x == 1) { $bg = "tdpersL"; } else { $bg = "tdpers"; }

$album_id = $row['album_id'];
$section_id = $row['section_id'];
	$queryD='SELECT * FROM ml_sections WHERE section_id = "' . $section_id . '"';
	$resultD= mysql_query($queryD);
	$rowD= mysql_fetch_array($resultD, MYSQL_NUM);
	$section_id_db = $rowD[0];
	$section_label_db = $rowD[1];
	$section_image_tag_db = $rowD[2];

$artist = $row['artist'];
$title = $row['title'];
$year1 = $row['year1'];
$year2 = $row['year2'];
$cancon_p = $row['cancon_p'];
$creation_date = substr ($row['creation_date'], 0, 10); 
$creation_date_display = date( "F" , mktime( 0 , 0 , 0 , substr($creation_date,5,2))).
" ".
substr($creation_date,8,2).
", ".
substr($creation_date,0,4);

$playbox_p = $row['playbox_p'];
$playbox_date = substr ($row['playbox_date'], 0, 10);
$cancon_p = $row['cancon_p'];
$cancon_p = $row['cancon_p'];

if ($playbox_p == "P" ) { $pb_mess = "Playbox"; } else { $pb_mess = "Library"; }
if ($cancon_p == "Y") { $flag = "<img src='images/National-Canadian-Flag.gif' />"; } else {$flag = '';}

$myURL = "edit_album.php?mode=edit&aid=$album_id&filter=$filter";

 echo "<tr class = '$bg' onMouseOver=\"this.className='tdH';\" onMouseOut=\"this.className='$bg'\" onClick=\"window.location.href='$myURL'; \">
    <td >
	<table cellspace='0' cellpad='2'>
	<tr><td background='$section_image_tag_db' nowrap='nowrap' align='center' onmouseover=\"Tip('Category: $section_label_db')\" onmouseout=\"UnTip()\"><img src='$section_image_tag_db' width='15' height='1'/><br><font color='white'>$section_id</font></td><td>&nbsp;$flag</td></tr></table>
	</td>
    <td >
	&nbsp;<strong>$title </strong>
	</td>
    <td >$artist</td>
    <td nowrap='nowrap'><font size='1'><b>$creation_date_display</b></font></td>
    <td >$pb_mess</td>
  </tr>";
  }
 
  
  ?>
</table>
<p>&nbsp;</p>

<p><strong><font face="Helvetica Neue, Arial, Helvetica"></font></strong></p>


<script type="text/javascript">
	var options = {
		script:"autosuggest/test.php?json=true&limit=6&",
		varname:"input",
		json:true,
		shownoresults:false,
		maxresults:12,
		callback: function (obj) {  }
	};
	var as_json = new bsn.AutoSuggest('testinput', options);
	
	
	var options_xml = {
		script: function (input) { return "autosuggest/test.php?input="+input+"&testid="+document.getElementById('testid').value; },
		varname:"input"
	};
	var as_xml = new bsn.AutoSuggest('testinput_xml', options_xml);
</script>
</body>
</html>
